//
// Created by liweimin on 2021/7/2.
//

#ifndef DATA_STRUCTURE_CIRCULAR_QUEUE_H
#define DATA_STRUCTURE_CIRCULAR_QUEUE_H

#endif //DATA_STRUCTURE_CIRCULAR_QUEUE_H
#define Element int
#define  MAX_SIZE 5//最大队列长度

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    Element *base;
    int front;//头指针
    int rear;//尾指针
} CQueue;

typedef struct
{
    int front;
    int rear;
    bool status;//记录队列是否为空 出队时判断 front==rear 置空（false） 入队时判断front==rear 置满（true）
    Element *base;
} Queue;


bool init(CQueue *queue)
{
    queue->base = (Element *) malloc(MAX_SIZE * sizeof(Element));
    if (!queue->base)
    {
        return false;
    }
    queue->front = queue->rear = 0;
    return true;
}

int length(CQueue *queue)
{
    return (queue->rear - queue->front) % MAX_SIZE;
}

bool isEmpty(CQueue *queue)
{
    return (queue->front == queue->rear);
}

bool isFull(CQueue *queue)
{
    return ((queue->rear + 1 - queue->front) % MAX_SIZE == 0);
}

bool EnQueue(CQueue *queue, Element e)
{
    if (isFull(queue))
    {
        printf("队列已满\n");
        return false;
    }
    queue->base[queue->rear] = e;
    queue->rear = (queue->rear + 1) % MAX_SIZE;
    return true;
}

bool DeQueue(CQueue *queue, Element *e)
{
    if (isEmpty(queue))
    {
        printf("队列为空\n");
        return false;
    }
    *e = queue->base[queue->front];
    queue->front = (queue->front + 1) % MAX_SIZE;
    return true;
}